﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDSP.BRL;
using System.Text.RegularExpressions;
using ImplementationInitialization.Constants;

namespace ImplementationInitialization
{
    /// <summary>
    /// <法规名，法规>键值对。
    /// <标准名,标准>键值对。
    /// <标准名称，标准>键值对。
    /// </summary>
    public class BaseDataInitial
    {
        static IDictionary<string, Catalog> _catalogDic = InitialCatalogs();
        static IDictionary<string, Category> _categoryDic = InitialCategories();
        static IDictionary<string, Codification> _existedCodification = InitialExistedCodification();
        static IDictionary<string, ExposureDraft> _existedExposureDraft = InitialExistedExposureDraft();

        private static IDictionary<string, ExposureDraft> InitialExistedExposureDraft()
        {
            IDictionary<string, ExposureDraft> exposureDic = new Dictionary<string, ExposureDraft>();
            List<ExposureDraft> exposures = new ExposureDraft().GetSpecialList<ExposureDraft>(null, null);
            foreach (ExposureDraft ed in exposures)
            {
                try
                {
                    AddExposure(exposureDic, ed);
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(DateTime.Now + " : Error - " + ex.Message);
                }
            }

            return exposureDic;
        }

        private static IDictionary<string, Codification> InitialExistedCodification()
        {
            IDictionary<string, Codification> codificationDic = new Dictionary<string, Codification>();
            List<Codification> codifications = new Codification().GetSpecialList<Codification>(null, null);
            foreach (Codification cod in codifications)
            {
                try
                {
                    Add(codificationDic, cod);
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(DateTime.Now + " : Error - " + ex.Message);
                }
            }

            return codificationDic;
        }

        public static void Add(IDictionary<string, Codification> codificationDic, Codification cod)
        {
            if (cod.DocType == DocType.标准)
            {
                //截取标准号做键。
                codificationDic.Add(cod.ReferenceNumber.Trim(), cod);

                if (cod.ReferenceNumber.Contains("/"))
                {
                    //截取修正版标准号做键。
                    //为了使系统里的标准号，跟标准的PDF文档里的标准号对应。标准的PDF文档里不能有"/",故以" "代替。
                    codificationDic.Add(cod.ReferenceNumber.Trim().Replace("/", " "), cod);
                }
            }
            else if (cod.DocType == DocType.法律法规)
            {
                //截取中文名做键。
                if (cod.Name.Contains(SpecialMark.SPLITTER_ENTER))
                {
                    codificationDic.Add(cod.Name.Substring(0, cod.Name.IndexOf(SpecialMark.SPLITTER_ENTER)).Trim(), cod);
                }
                else if (cod.Name.Contains(SpecialMark.SPLITTER_BLANK))
                {
                    codificationDic.Add(cod.Name.Substring(0, cod.Name.IndexOf(SpecialMark.SPLITTER_BLANK)).Trim(), cod);
                }
                else
                {
                    //初始全名做键。
                    codificationDic.Add(cod.Name.Trim(), cod);
                }
            }
        }

        public static void AddExposure(IDictionary<string, ExposureDraft> exposureDic, ExposureDraft ed)
        {
            //截取中文名做键。
            if (ed.Name.Contains(SpecialMark.SPLITTER_ENTER))
            {
                exposureDic.Add(ed.Name.Substring(0, ed.Name.IndexOf(SpecialMark.SPLITTER_ENTER)).Trim(), ed);
            }
            else if (ed.Name.Contains(SpecialMark.SPLITTER_BLANK))
            {
                exposureDic.Add(ed.Name.Substring(0, ed.Name.IndexOf(SpecialMark.SPLITTER_BLANK)).Trim(), ed);
            }
            else
            {
                //初始全名做键。
                exposureDic.Add(ed.Name.Trim(), ed);
            }
        }

        private static IDictionary<string, Catalog> InitialCatalogs()
        {
            IDictionary<string, Catalog> catalogDic = new Dictionary<string, Catalog>();
            IDictionary<int, Catalog> catalogIDDic = new Dictionary<int, Catalog>();

            List<Catalog> catalogs = new Catalog().GetAllCatalogs();

            //构造name为Key的字典。
            //构造id为Key的字典。
            foreach (Catalog l in catalogs)
            {
                if (!catalogDic.ContainsKey(l.Name))
                {
                    catalogDic.Add(l.Name, l);
                    catalogDic.Add(l.ID.ToString(), l);
                }
                else if (l.ParentID == 0)
                {//若是根目录，则强制覆盖已有的字典值。
                    catalogDic[l.Name] = l;
                }

                catalogIDDic.Add(l.ID, l);
            }

            //构造全路径为Key的字典。
            try
            {
                foreach (Catalog l in catalogs)
                {//只有是子节点的，这里才需要构造。
                    if (l.ParentID > 0)
                    {
                        string fullName = GetFullName(catalogIDDic, l.FullPath);
                        if (!catalogDic.ContainsKey(fullName))
                        {
                            catalogDic.Add(fullName, l);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return catalogDic;
        }

        private static string GetFullName(IDictionary<int, Catalog> catalogIDDic, string p)
        {
            string fullName = string.Empty;
            string[] ids = p.Split('-');
            for (int i = 0; i < ids.Length; i++)
            {
                int id = 0;
                if (!string.IsNullOrEmpty(ids[i]) && Int32.TryParse(ids[i], out id))
                {
                    try
                    {
                        fullName += catalogIDDic[id].Name + "-";
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }

            if (fullName.EndsWith("-"))
            {
                fullName = fullName.Remove(fullName.Length - 1, 1);
            }

            return fullName;
        }

        private static IDictionary<string, Category> InitialCategories()
        {
            IDictionary<string, Category> categoryDic = new Dictionary<string, Category>();

            List<Category> categories = new Category().GetAllCategories();
            foreach (Category c in categories)
            {
                if (!categoryDic.ContainsKey(c.Name))
                {
                    categoryDic.Add(c.Name, c);
                }
            }

            return categoryDic;
        }

        public static IDictionary<string, Catalog> GetCatalogs()
        {
            return _catalogDic;
        }

        public static IDictionary<string, Category> GetCategories()
        {
            return _categoryDic;
        }

        public static IDictionary<string, Codification> GetExistedCodification()
        {
            return _existedCodification;
        }

        public static IDictionary<string, ExposureDraft> GetExistedExposureDraft()
        {
            return _existedExposureDraft;
        }
    }
}
